package zl.ordermanager.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import zl.ordermanager.service.CustomerCallService;
import zl.ordermanager.vo.CustomerCall;
import zl.ordermanager.vo.DataGrid;
import zl.ordermanager.vo.EContactResult;
import zl.ordermanager.vo.ECustomerType;
import zl.ordermanager.vo.PageHelper;
import zl.ordermanager.vo.Total;

@Controller
@RequestMapping("/customerCall")
public class CustomerCallController extends BaseController {

	@Autowired
	private CustomerCallService customerCallService;

	@RequestMapping("/showList")
	public String showList(HttpSession session, PageHelper pageHelper, Model model) {
		Total total = new Total();
		DataGrid<CustomerCall> dataGrid = customerCallService.dataGrid(session, pageHelper, total);
		List<CustomerCall> customerCallList = dataGrid.getList();
		long totalCount = dataGrid.getTotal();

		model.addAttribute("customerCallList", customerCallList);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageHelper", pageHelper);
		model.addAttribute("total", total);
		model.addAttribute("resultList", EContactResult.values());

		return "customerCallList";
	}

	@RequestMapping("/showAdd")
	public String showAdd(HttpSession session, Model model) {
		model.addAttribute("customerTypeList", ECustomerType.values());
		model.addAttribute("resultList", EContactResult.values());
		model.addAttribute("currentDate", new SimpleDateFormat("yyyy-M-d").format(new Date()));

		return "customerCallAdd";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ModelAndView add(CustomerCall customerCall) {
		try {
			customerCallService.add(customerCall);
		} catch (Exception e) {
			return ajaxDoneError(e.getMessage());
		}

		return ajaxDoneSuccess("操作成功");
	}

	@RequestMapping("/showEdit/{customerCallId}")
	public String showEdit(@PathVariable("customerCallId") Integer customerCallId, Model model) {
		CustomerCall customerCall = customerCallService.get(customerCallId);

		model.addAttribute("customerCall", customerCall);
		model.addAttribute("customerTypeList", ECustomerType.values());
		model.addAttribute("resultList", EContactResult.values());

		return "customerCallEdit";
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public ModelAndView edit(CustomerCall customerCall) {
		try {
			customerCallService.edit(customerCall);
		} catch (Exception e) {
			return ajaxDoneError(e.getMessage());
		}
		return ajaxDoneSuccess("操作成功");
	}

	@RequestMapping("/delete/{customerCallId}")
	public ModelAndView delete(@PathVariable("customerCallId") Integer customerCallId) {

		customerCallService.delete(customerCallId);

		return ajaxDoneSuccess("操作成功");
	}
}
